import os

def dialogue_text(input_file, output_file):
    # 初始化
    process_next = False
    is_d = False
    is_e = False
    is_word = False
    
    with open(input_file, "r") as f:
        lines = f.readlines()
    
    output = ""
    start = "<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:mstts=\"http://www.w3.org/2001/mstts\" xmlns:emo=\"http://www.w3.org/2009/10/emotionml\" version=\"1.0\" xml:lang=\"en-US\">"
    output += start + "\n\n"

    for line in lines:
        if line.startswith("#word"):
            # 表示要跳过该行，同时说明下一行是单词
            process_next = True
            is_word = True
        elif line.startswith("#d"):
            # 以#d开头，表示要跳过该行，同时说明下一行是描述
            process_next = True
            is_d = True
        elif line.startswith("#e"):
            # 以#e开头，表示要跳过该行，同时说明下一行是例子
            process_next = True
            is_e = True
        elif process_next:
            if is_word:
                speaker_word_list = ["en-US-JaneNeural", "en-US-JasonNeural"]
                for i in range(0, 2):
                    output += "<voice name=\"" + speaker_word_list[i % 3] + "\"><prosody rate=\"-5%\" pitch=\"0%\">\n" + line + "</prosody></voice>\n"
                    if i == 1:
                        output += "\n"
                # Reset the flag
                process_next = False
                is_word = False
            elif is_d:
                speaker_d_list = ["en-US-SaraNeural", "en-US-AriaNeural"]
                for i in range(0, 2):
                    output += "<voice name=\"" + speaker_d_list[i % 2] + "\"><prosody rate=\"-5%\" pitch=\"0%\">\n" + line + "</prosody></voice>\n"
                    if i == 1:
                        output += "\n"
                # Reset the flag
                process_next = False
                is_d = False
            elif is_e:
                speaker_e_list = ["en-US-JennyNeural", "en-US-GuyNeural", "en-US-NancyNeural"]
                style_list = ["chat", "hopeful", "cheerful"]
                for i in range(0, 3):
                    output += "<voice name=\"" + speaker_e_list[i % 3] + "\"><mstts:express-as style=\"" + style_list[i % 3] + "\"><prosody rate=\"-5%\" pitch=\"0%\">\n" + line + "</prosody></mstts:express-as></voice>\n"
                    if i == 2:
                        output += "\n"
                # Reset the flag
                process_next = False
                is_e = False
    
    end = "</speak>"
    output += end

    with open(output_file, "w") as f:
        f.write(output)


# 获取该.py文件的绝对路径，如：d:\vocabulary\text.py
current_file_path = os.path.abspath(__file__)
# 获取该.py文件所在路径，如：d:\vocabulary
current_directory = os.path.dirname(current_file_path)
# 输入文件夹
input_path = os.path.join(current_directory, "input")
# 输出在output文件夹下
output_path = os.path.join(current_directory, "output")


for file_name in os.listdir(input_path): # 获取文件夹下所有文件名
    if file_name.endswith('.txt'): # 判断文件是否为txt文件
        input_file = os.path.join(input_path, file_name) # 获取文件路径

        # 写入处理后的内容到新的文件
        new_file_name = file_name.replace('.txt', '_output.txt') # 生成新的文件名
        output_file = os.path.join(output_path, new_file_name)
        # 对内容进行处理
        processed_content = dialogue_text(input_file, output_file)

